﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KBManagement.DAL;
using KBManagement.BIZ;
using KBManagement.Library;
namespace KBManagement
{
    public partial class FoodManagement : Form,InforObserver
    {
        KBSQLClassDataContext context;
        FoodCategoryFacade foodCategoryFacade;
        FoodFacade foodFacade;
        private string idCateService;
        public FoodManagement()
        {
            foodFacade = new FoodFacade();
            foodCategoryFacade = new FoodCategoryFacade();
            Utils.CreateInstance().AddInforObserver(this);
            context = Utils.CreateInstance().GetDBContext();
            InitializeComponent();
            dataGridView1.CellBeginEdit += new DataGridViewCellCancelEventHandler(dataGridView1_CellBeginEdit);
            dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellClick);
            SelectFoodCategory();
            SelectFood();
        }


        void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            
            toolStripBtnEditCateService.Enabled = true;
            sửaLoạiDịchVụToolStripMenuItem.Enabled = true;
            mnAddComponent.Enabled = false;
            sửaToolStripMenuItem.Enabled = false;
            toolStripBtnAddCompo.Enabled = false;
            toolStripBtnEditService.Enabled = false;
            try
            {
                SelectFoodByCaId(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
                idCateService = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
            }
            catch (Exception)
            {
            }
        }

        void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            if (e.ColumnIndex == 0)
            {
                e.Cancel = true;
            }
        }

      

        private void thêmDịchVụToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddNewService();
        }

        private void btnAddService_Click(object sender, EventArgs e)
        {
            AddNewService();
        }

        private void thêmLoạiDịchVụToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddCategoryService();
        }

        private void btnAddCateService_Click(object sender, EventArgs e)
        {
            AddCategoryService();
        }

        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            toolStripBtnAddCompo.Enabled = true;
            toolStripBtnEditService.Enabled = true;
            mnAddComponent.Enabled = true;
            sửaToolStripMenuItem.Enabled = true;
        }

        private void btnEditCateService_Click(object sender, EventArgs e)
        {
            EditCateGoryService();
        }


        private void sửaLoạiDịchVụToolStripMenuItem_Click(object sender, EventArgs e)
        {
            EditCateGoryService();
        }



        public void Update()
        {
            SelectFood();
            SelectFoodCategory();
        }


        void SelectFoodByCaId(string id)
        {
            var foods = from fc in context.Foods
                        where fc.FoodCategoryID.Value.ToString().Equals(id)
                        select new
                        {
                            fc.ID,
                            fc.Fo_Name,
                            fc.Price,
                            fc.Status,
                            fc.FoodCaterory.Name
                        };
            dataGridView2.DataSource = foods;
            dataGridView2.Columns[0].HeaderText = "Mã số";
            dataGridView2.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            dataGridView2.Columns[1].HeaderText = "Tên dịch vụ";
            dataGridView2.Columns[2].HeaderText = "Giá tiền";
            dataGridView2.Columns[3].HeaderText = "Tình trạng";
            dataGridView2.Columns[4].HeaderText = "Loại dịch vụ";
        }

        private void SelectFood()
        {
            var foods = from fc in context.Foods
                        select new
                        {
                            fc.ID,
                            fc.Fo_Name,
                            fc.Price,
                            fc.Status,
                            fc.FoodCaterory.Name
                        };
            dataGridView2.DataSource = foods;
            dataGridView2.Columns[0].HeaderText = "Mã số";
            dataGridView2.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            dataGridView2.Columns[1].HeaderText = "Tên dịch vụ";
            dataGridView2.Columns[2].HeaderText = "Giá tiền";
            dataGridView2.Columns[3].HeaderText = "Tình trạng";
            dataGridView2.Columns[4].HeaderText = "Mã loại dịch vụ";

        }
        private void SelectFoodCategory()
        {
            var foodCategorys = from fc in context.FoodCaterories select fc;
            dataGridView1.DataSource = foodCategorys;
            dataGridView1.ColumnHeadersHeight = 30;
            dataGridView1.Columns[0].HeaderText = "Mã số";
            dataGridView1.Columns[1].HeaderText = "Tên loại dịch vụ";
            dataGridView1.Columns[2].HeaderText = "Tình trạng";
        }

        private void EditCateGoryService()
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                try
                {
                    FoodCaterory foodCategory = foodCategoryFacade.Find(dataGridView1.SelectedRows[0].Cells[0].Value.ToString());
                    DialogAddCateService add = new DialogAddCateService(foodCategory);
                    add.ShowDialog();
                }
                catch (Exception)
                {
                }
            }
        }

        private static void AddCategoryService()
        {
            DialogAddCateService add = new DialogAddCateService(null);
            add.ShowDialog();
        }

        private void AddNewService()
        {
            DialogAddService add = new DialogAddService(null, idCateService);
            add.ShowDialog();
        }

        private void EditService()
        {
            if (dataGridView2.SelectedRows.Count > 0)
            {
                try
                {
                    Food food = foodFacade.Find(dataGridView2.SelectedRows[0].Cells[0].Value.ToString());
                    DialogAddService add = new DialogAddService(food, idCateService);
                    add.ShowDialog();
                }
                catch (Exception)
                {
                }
            }
        }

        private void btneditService_Click(object sender, EventArgs e)
        {
            EditService();
        }

        private void sửaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            EditService();
        }

        private void toolStripBtnAddCompo_Click(object sender, EventArgs e)
        {
            EditComponent();
        }

        private void EditComponent()
        {
            if (dataGridView2.SelectedRows.Count > 0)
            {
                try
                {
                    var food = foodFacade.Find(dataGridView2.SelectedRows[0].Cells[0].Value);
                    AddEditComponent addEditComponent = new AddEditComponent(food);
                    addEditComponent.ShowDialog();

                }
                catch (Exception)
                {
                }
            }
        }

        private void dataGridView2_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            EditComponent();
        }

        private void mnAddComponent_Click(object sender, EventArgs e)
        {
            EditComponent();
        }

        private void hiểnThịChiTiếtToolStripMenuItem_Click(object sender, EventArgs e)
        {
            EditComponent();
        }

    }
}
